﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using JobSiteStarterKit.ENUM;
using JobSiteStarterKit.BOL;

public partial class Admin_NewJobPost : System.Web.UI.Page
{
    protected string sBaseUrl = ConfigurationManager.AppSettings["WC_baseurl"];
    private DBWork dbWork;
    public DBWork DbWork
    {
        get
        {
            return dbWork;
        }
        set
        {
            dbWork = value;
        }
    }
   

    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
            drpCountry.DataSource = Country.GetCountries();
            drpCountry.DataTextField = "CountryName";
            drpCountry.DataValueField = "CountryID";
            drpCountry.DataBind();

            drpJobType.DataSource = JobType.GetJobTypes();
            drpJobType.DataTextField = "JobTypeName";
            drpJobType.DataValueField = "JobTypeID";
            drpJobType.DataBind();

            lstEducation.DataSource = EducationLevel.GetEducationLevels();
            lstEducation.DataTextField = "EducationLevelName";
            lstEducation.DataValueField = "EducationLevelID";
            lstEducation.DataBind();

            drpJobCategory.DataSource = Industry.GetIndustry();
            drpJobCategory.DataTextField = "CategoryName";
            drpJobCategory.DataValueField = "CategoryID";
            drpJobCategory.DataBind();

            drpJobFunctionalArea.DataSource = JobSiteStarterKit.BOL.FunctionalArea.GetAllFunctionalArea();
            drpJobFunctionalArea.DataTextField = "SubcategoryName";
            drpJobFunctionalArea.DataValueField = "SubcategoryID";
            drpJobFunctionalArea.DataBind();

            drpState.DataSource = JobSiteStarterKit.BOL.State.GetStates();
            drpState.DataTextField = "StateName";
            drpState.DataValueField = "StateID";
            drpState.DataBind();

            //set the company is to the viewstate of control
            int CompanyId = 0;
           // JobSiteStarterKit.BOL.Company company = JobSiteStarterKit.BOL.Company.GetCompany(Context.User.Identity.Name);
            JobSiteStarterKit.BOL.Company company = JobSiteStarterKit.BOL.Company.GetCompany("ravi");// hard coded company user name
            CompanyId = company.CompanyID;
            ViewState["Company_Id"] = CompanyId;

            if (Request.QueryString["PostId"] != null && Request.QueryString["PostId"] != "")
            {
                PopulatePostedJobDetail(Request.QueryString["PostId"]);
            }
        }
    }


    public JobPosting InitializeJob()
    {
        JobPosting objJob = new JobPosting();
        if (Request.QueryString["PostId"] != null && Request.QueryString["PostId"] != "")
        {
            objJob.JobPostingID = Convert.ToInt32(Request.QueryString["PostId"]);
        }
        objJob.Category = Convert.ToInt32(drpJobCategory.SelectedItem.Value);
        objJob.City = txtCity.Text.Trim();    
        string strOpeningdate = txtJobOpeningDate.Text.Trim();
        System.Globalization.DateTimeFormatInfo dateInfo1 = new System.Globalization.DateTimeFormatInfo();
        dateInfo1.ShortDatePattern = "dd/MM/yyyy";
        objJob.OpeningDate = Convert.ToDateTime(strOpeningdate, dateInfo1); 

        string strClosingdate = txtJobClosingDate.Text.Trim();
        System.Globalization.DateTimeFormatInfo dateInfo2 = new System.Globalization.DateTimeFormatInfo();
        dateInfo2.ShortDatePattern = "dd/MM/yyyy";
        objJob.ClosingDate = Convert.ToDateTime(strClosingdate, dateInfo2); 
        //objJob.CompanyID = ;
        objJob.Compensation = txtCompensation.Text.Trim();
        objJob.ContactPerson = txtContactPerson.Text;
        objJob.CountryID = Convert.ToInt32(drpCountry.SelectedItem.Value);
        objJob.Department = drpJobCategory.SelectedItem.Text;
        objJob.Description = txtJobDesc.Text.Trim();
        string stredu = "";
        foreach (ListItem itm in lstEducation.Items)
        {
            if (itm.Selected == true)
            {
                stredu += itm.Value + ",";
            }
        }
        if (stredu != "")
        {
            stredu = stredu.Substring(0, stredu.LastIndexOf(','));
        }
        if (txtEducationOthers.Visible == true)
        {
            stredu += "," + txtEducationOthers.Text.Trim();
        }
        objJob.EducationalOthers = stredu;
        if (chkFreshers.Checked == true)
            objJob.EducationLevelID = Convert.ToInt32(chkFreshers.ToolTip);
        objJob.FunctionalArea = drpJobFunctionalArea.SelectedItem.Text;
        objJob.JobCode = txtJobCode.Text.Trim();
        objJob.JobTypeID = Convert.ToInt32(drpJobType.SelectedItem.Value);
        // try to convert the numeric value

        bool bln1, bln2;
        double num1, num2;
        bln1 = double.TryParse(txtMaxExpMonth.Text.Trim(), out num1);
        bln2 = double.TryParse(txtMaxExpYears.Text.Trim(), out num2);
        if (bln1 == true)
            objJob.MaxExp = Convert.ToInt32(txtMaxExpMonth.Text.Trim());
        if (bln2)
        {
            objJob.MaxExp += Convert.ToInt32(txtMaxExpYears.Text.Trim()) * 12;
        }

        double maxSal;
        bln1 = double.TryParse(txtMaxSalary.Text.Trim(), out maxSal);

        if (bln1)
            objJob.MaxSalary = Convert.ToDecimal(txtMaxSalary.Text.Trim());
        double minExpMonth, minExpYear;
        bln1 = double.TryParse(txtMinExpMonth.Text.Trim(), out minExpMonth);
        bln2 = double.TryParse(txtMinExpYears.Text.Trim(), out minExpYear);
        if (bln1)
            objJob.MinExp = Convert.ToInt32(txtMinExpMonth.Text.Trim());
        if (bln2)
            objJob.MinExp += Convert.ToInt32(txtMinExpYears.Text.Trim()) * 12;

        double minSal;
        bln1 = double.TryParse(txtMaxSalary.Text.Trim(), out minSal);
        if (bln1)
            objJob.MinSalary = Convert.ToDecimal(txtMinSalary.Text.Trim());

     //   objJob.OpeningDate = Convert.ToDateTime(txtJobOpeningDate.Text.Trim());
       

        objJob.StateID = Convert.ToInt32(drpState.SelectedItem.Value);
        objJob.Title = txtJobTitle.Text.Trim();
        if (txtContactPersonEmail.Text.Trim() != "")
            objJob.ContactPersonEmail = txtContactPersonEmail.Text.Trim();
        if (ViewState["Company_Id"] != null)
            objJob.CompanyID = Convert.ToInt32(ViewState["Company_Id"]);
        return objJob;

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //if (dbWork == DBWork.Insert)
        //{
        //    JobPosting.Insert(InitializeJob());
        //    //redirect to the page that tells the job is posted successfully.
        //    // possibly my accounts.
        //    Response.Redirect("../Employer/myaccounts.aspx");
        //}
        //else if (dbWork == DBWork.Update)
        //    JobPosting.Update(InitializeJob());
        //else if (dbWork == DBWork.Delete)
        //{

        //}
        if (Request.QueryString["PostId"] == null)
        {
            JobPosting.Insert(InitializeJob());
            //Response.Redirect("./hotjobs.aspx");
            Response.Write("<script>alert('Job Post Saved Successfully.');top.location.replace('hotjobs.aspx');</script>");
        }
        else if (Request.QueryString["PostId"] != null && Request.QueryString["PostId"] != "")
        {
            JobPosting.JobUpdate(InitializeJob());
            //Response.Redirect("./hotjobs.aspx");
            Response.Write("<script>alert('Job Post Edited Successfully.');top.location.replace('hotjobs.aspx');</script>");
        }
        

    }
    protected void drpCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryId = Convert.ToInt32(drpCountry.SelectedItem.Value);
        PopulateStates(CountryId);
       
    }
    protected void PopulateStates(int CountryId)
    {
        drpState.DataSource = State.GetStates(CountryId);
        drpState.DataTextField = "StateName";
        drpState.DataValueField = "StateID";
        drpState.DataBind();
    }

    protected void drpJobFunctionalArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpJobFunctionalArea.SelectedItem.Value == "-1")
        {
            txtOthersFunctionalArea.Visible = true;
        }
        else
        {
            txtOthersFunctionalArea.Visible = false;
        }
    }

    protected void lstEducation_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (lstEducation.SelectedItem != null)
        {
            if (lstEducation.SelectedItem.Value == "-1")
                txtEducationOthers.Visible = true;
            else
                txtEducationOthers.Visible = false;
        }
    }
    protected void drpJobCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        drpJobFunctionalArea.DataSource = FunctionalArea.GetFunctionalArea(Convert.ToInt32(drpJobCategory.SelectedItem.Value));
        drpJobFunctionalArea.DataTextField = "SubCategoryName";
        drpJobFunctionalArea.DataValueField = "SubCategoryId";
        drpJobFunctionalArea.DataBind();
    }

    protected void PopulatePostedJobDetail(string postingid)
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
        obj1.Add("@PostingId", SqlDbType.NVarChar, 2000).Value = postingid;
        DataSet ds = new DataSet();
        ds = obj.ExecuteDataSet("proc_GetJobPostingDetailsByPostingID");
        if (ds != null)
        {
            if(ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                //objJobPosting.JobPostingID = Dreader.GetInt32(Dreader.GetOrdinal("PostingID"));
                //objJobPosting.CompanyID = Dreader.GetInt32(Dreader.GetOrdinal("CompanyID"));
                if (ds.Tables[0].Rows[0]["JobCode"] != DBNull.Value)
                {
                    txtJobCode.Text = Convert.ToString(ds.Tables[0].Rows[0]["JobCode"]);
                }
                if (ds.Tables[0].Rows[0]["Title"] != DBNull.Value)
                {
                    txtJobTitle.Text = Convert.ToString(ds.Tables[0].Rows[0]["Title"]);
                }
                if (ds.Tables[0].Rows[0]["JobDescription"] != DBNull.Value)
                {
                    txtJobDesc.Text = Convert.ToString(ds.Tables[0].Rows[0]["JobDescription"]);
                }
                if (ds.Tables[0].Rows[0]["OpeningDate"] != DBNull.Value)
                {
                    DateTime OpeningDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["OpeningDate"]);
                    txtJobOpeningDate.Text = OpeningDate.ToString("dd-MM-yyyy");
                   // txtJobOpeningDate.Text = Convert.ToString(Convert.ToDateTime(ds.Tables[0].Rows[0]["OpeningDate"]));
                }
                if (ds.Tables[0].Rows[0]["ClosingDate"] != DBNull.Value)
                {
                    DateTime ClosingDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["ClosingDate"]);
                    txtJobClosingDate.Text = ClosingDate.ToString("dd-MM-yyyy");
                   // txtJobClosingDate.Text = Convert.ToString(Convert.ToDateTime(ds.Tables[0].Rows[0]["ClosingDate"]));
                }
                if (ds.Tables[0].Rows[0]["CountryID"] != DBNull.Value)
                {
                    drpCountry.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["CountryID"]);
                    PopulateStates(Convert.ToInt32(ds.Tables[0].Rows[0]["CountryID"]));
                }
                if (ds.Tables[0].Rows[0]["StateID"] != DBNull.Value)
                {
                    drpState.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["StateID"]);
                }
                if (ds.Tables[0].Rows[0]["City"] != DBNull.Value)
                {
                    txtCity.Text = Convert.ToString(ds.Tables[0].Rows[0]["City"]);
                }
                if (ds.Tables[0].Rows[0]["JobTypeID"] != DBNull.Value)
                {
                    drpJobType.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["JobTypeID"]);
                }
                if (ds.Tables[0].Rows[0]["CategoryID"] != DBNull.Value)
                {
                    drpJobCategory.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["CategoryID"]);
                }
                if (ds.Tables[0].Rows[0]["FunctionalArea"] != DBNull.Value)
                {
                    drpJobFunctionalArea.SelectedItem.Text = Convert.ToString(ds.Tables[0].Rows[0]["FunctionalArea"]);
                }
                if (ds.Tables[0].Rows[0]["ContactPerson"] != DBNull.Value)
                {
                    txtContactPerson.Text = Convert.ToString(ds.Tables[0].Rows[0]["ContactPerson"]);
                }              

                if (ds.Tables[0].Rows[0]["ContactPersonEmail"] != DBNull.Value)
                {
                    txtContactPersonEmail.Text = Convert.ToString(ds.Tables[0].Rows[0]["ContactPersonEmail"]);
                }
                if (ds.Tables[0].Rows[0]["MinSalary"] != DBNull.Value)
                {
                    txtMinSalary.Text = Convert.ToString(ds.Tables[0].Rows[0]["MinSalary"]);
                }
                if (ds.Tables[0].Rows[0]["MaxSalary"] != DBNull.Value)
                {
                    txtMaxSalary.Text = Convert.ToString(ds.Tables[0].Rows[0]["MaxSalary"]);
                }
                if (ds.Tables[0].Rows[0]["MinExp"] != DBNull.Value)
                {
                    if (Convert.ToInt32(ds.Tables[0].Rows[0]["MinExp"]) > 11)
                    {
                        txtMinExpYears.Text = Convert.ToString( Convert.ToInt32(ds.Tables[0].Rows[0]["MinExp"]) / 12);
                        txtMinExpMonth.Text = Convert.ToString(Convert.ToInt32(ds.Tables[0].Rows[0]["MinExp"]) % 12);
                      
                    }
                    else
                    {
                        txtMinExpMonth.Text = Convert.ToString(ds.Tables[0].Rows[0]["MinExp"]);
                    }
                }
              
                if (ds.Tables[0].Rows[0]["MaxExp"] != DBNull.Value)
                {
                    if (Convert.ToInt32(ds.Tables[0].Rows[0]["MaxExp"]) > 11)
                    {
                        txtMaxExpYears.Text = Convert.ToString(Convert.ToInt32(ds.Tables[0].Rows[0]["MaxExp"]) / 12);
                        txtMaxExpMonth.Text = Convert.ToString(Convert.ToInt32(ds.Tables[0].Rows[0]["MaxExp"]) % 12);

                    }
                    else
                    {
                        txtMaxExpMonth.Text = Convert.ToString(ds.Tables[0].Rows[0]["MinExp"]);
                    }                 
                }

                if (ds.Tables[0].Rows[0]["EducationLevelID"] != DBNull.Value)
                {
                    if (Convert.ToBoolean(ds.Tables[0].Rows[0]["EducationLevelID"]))
                    {
                        chkFreshers.Checked = true;
                    }
                    else
                    {
                        chkFreshers.Checked = false;
                    }
                }
                else
                {
                    chkFreshers.Checked = false;
                }


                if (ds.Tables[0].Rows[0]["EducationLevelOthers"] != DBNull.Value)
                {
                    string strLevelOthers = Convert.ToString(ds.Tables[0].Rows[0]["EducationLevelOthers"]);
                    string[] words = strLevelOthers.Split(',');
                    for (int i = 0; i < words.Length; i++)
                    {
                        for (int j = 0; j < lstEducation.Items.Count; j++)
                        {
                            if (words[i].ToString() == lstEducation.Items[j].Value)
                            {
                                lstEducation.Items[j].Selected = true;
                            }

                        }

                    }
                    
                }
                if (ds.Tables[0].Rows[0]["Compensation"] != DBNull.Value)
                {
                    txtCompensation.Text = Convert.ToString(ds.Tables[0].Rows[0]["Compensation"]);
                }
               
                //txtMinSalary.Text = Dreader.GetDecimal(Dreader.GetOrdinal("MinSalary"));
                //txtMaxSalary.Text = Dreader.GetDecimal(Dreader.GetOrdinal("MaxSalary"));
                //txtJobOpeningDate = Dreader.GetDateTime(Dreader.GetOrdinal("PostingDate"));
                //objJobPosting.PostedBy = Dreader.GetString(Dreader.GetOrdinal("PostedBy"));
            }
           
        }

    }
    
}
